

package com.hazelcast.client.connectionstrategy;

import com.hazelcast.client.config.ClientClasspathXmlConfig;
import com.hazelcast.client.test.ClientTestSupport;
import com.hazelcast.client.test.TestHazelcastFactory;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.LifecycleEvent;
import com.hazelcast.core.LifecycleListener;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.After;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;

import static com.hazelcast.core.LifecycleEvent.LifecycleState.CLIENT_CONNECTED;
import static org.junit.Assert.assertTrue;

@RunWith(HazelcastParallelClassRunner.class)
@Category({QuickTest.class})
public class ConfiguredBehaviourTestXmlConfig extends ClientTestSupport {

    private static final CountDownLatch asyncStartFromXmlLatch = new CountDownLatch(1);
    private final TestHazelcastFactory hazelcastFactory = new TestHazelcastFactory();

    @After
    public void tearDown() {
        hazelcastFactory.terminateAll();
    }

    @Test
    public void testAsyncStartTrueXmlConfig() {
        ClientClasspathXmlConfig clientConfig = new ClientClasspathXmlConfig("hazelcast-client-connection-strategy-asyncStart-true.xml");

        HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);

        assertTrue(client.getLifecycleService().isRunning());

        hazelcastFactory.newHazelcastInstance();

        assertOpenEventually(asyncStartFromXmlLatch);

        client.getMap(randomMapName());
    }

    @Test
    public void testReconnectModeASYNCSingleMemberStartLateXmlConfig() {

        HazelcastInstance hazelcastInstance = hazelcastFactory.newHazelcastInstance();

        ClientClasspathXmlConfig clientConfig = new ClientClasspathXmlConfig("hazelcast-client-connection-strategy-asyncReconnect.xml");

        HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);

        assertTrue(client.getLifecycleService().isRunning());

        ReconnectListener reconnectListener = new ReconnectListener();
        client.getLifecycleService().addLifecycleListener(reconnectListener);

        hazelcastInstance.shutdown();
        hazelcastFactory.newHazelcastInstance();

        assertOpenEventually(reconnectListener.reconnectedLatch);

        client.getMap(randomMapName());
    }

    private static class AsyncStartListener implements LifecycleListener {
        @Override
        public void stateChanged(LifecycleEvent event) {
            if (event.getState() == CLIENT_CONNECTED) {
                asyncStartFromXmlLatch.countDown();
            }
        }
    }
}
